<?php
/**
 * Created by PhpStorm.
 * User: LiuJiaJun
 * Date: 2018/12/5
 * Time: 14:06
 */

namespace api\storage\service;

use api\storage\model\BomModel;

class BomService
{
    /**
     * bom单区分分隔符
     */
    const ADD_SEPARATOR = '+';

    /**
     * 判断订单号类型
     * @param string $orderId 订单号
     * @return array|string 返回数据
     */
    public static function judgeBomType($orderId = '')
    {
        if (empty($orderId)) {
            return '';
        }
        //通过'+'或者空格来判定订单类型，委外订单进行字符串截取
        /** @noinspection PhpLanguageLevelInspection */
        if (!empty(strpos($orderId, self::ADD_SEPARATOR))) {
            $order_id = strstr($orderId, self::ADD_SEPARATOR, true);
            $fentryid = substr(strstr($orderId, self::ADD_SEPARATOR), 1);
        } else {
            $order_id = $orderId;
            $fentryid = "";
        }
        return [$order_id, $fentryid];
    }

    /**
     * 根据orderId的信息获取bom的信息
     *
     * @param $bomId
     * @param $storageUuid
     * @return array|false|\PDOStatement|string|\think\Model
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public static function getBomInfo($bomId, $storageUuid = '')
    {
        if (empty($bomId)) {
            return [];
        }
        list($orderId, $fentryid) = self::judgeBomType($bomId);
        $where['order_id'] = $orderId;
        if (!empty($fentryid)) {
            $where['fentryid'] = $fentryid;
        }
        if (!empty($storageUuid)) {
            $where['storage_uuid'] = $storageUuid;
        }
        $bomModel = new BomModel();
        return $bomModel->findBomInfo($where);//获取bom单数据
    }

    public static function generateBom($orderId, $productType, $storageUuid, $remark = '', $status = 0)
    {
        return [
            'uuid' => create_uuid(),
            'status' => $status,
            'order_id' => $orderId,
            'product_type' => $productType,
            'storage_uuid' => $storageUuid,
            'remark' => $remark,
            'time' => get_current_time()
        ];
    }
}
